/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.core.cachepool.extend.connection.datasource;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.hibernate.cfg.Environment;

import com.jinhe.tss.core.ConfigurableContants;
import com.jinhe.tss.core.exception.BusinessException;

/**
 * <p> _Connection.java  </p>
 * 
 * @author Jon.King 2007-5-9
 */
public  class _Connection extends ConfigurableContants{
    
    protected final Logger log = Logger.getLogger(this.getClass());
    
    private IConnectionProvider provider;
    
    private _Connection() {
        if(properties.getProperty(Environment.DATASOURCE) != null){
            provider = new DatasourceConnectionProvider();
        }else{
            provider = new DriverManagerConnectionProvider();
        }
    }
    
    private static _Connection _connection;
    
    public static _Connection getInstanse(){
        if(_connection == null){
            _connection = new _Connection();
        }
        return _connection;
    }

    public Connection getConnection(){
        return provider.getConnection(properties);
    }

    public void releaseConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                log.fatal("销毁数据库连接时候出错", e);
                throw new BusinessException("销毁数据库连接时候出错", e);
            }
        }
    }
}